package com.miracle9.game.service;

import com.miracle9.game.dao.AdminServiceDao;
import com.miracle9.game.dao.SystemConfigServiceDao;
import com.miracle9.game.dao.UserRegistLogServiceDao;
import com.miracle9.game.bean.Pager;
import com.miracle9.game.entity.Admin;
import com.miracle9.game.entity.SystemConfig;
import com.miracle9.game.entity.User;
import com.miracle9.game.entity.UserRegistLog;
import com.miracle9.game.server.message.AmfMessageSend;
import com.miracle9.game.util.MyUtil;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.apache.mina.core.session.IoSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service("userRegistLogService")
public class UserRegistLogService {

    private static final Logger logger = Logger.getLogger(UserRegistLogService.class);
    @Autowired
    private UserRegistLogServiceDao userRegistLogServiceDao;
    @Autowired
    private AdminServiceDao adminServiceDao;
    @Autowired
    private SystemConfigServiceDao systemConfigServiceDao;
    @Autowired
    private UserService userService;
    public static Object verifyLock = new Object();

    public Map<String, Object> userRegistCountMap() {
        Map<String, Object> result = new HashMap();
        int nUserRegistCount = this.userRegistLogServiceDao.getQuantityByApplyStatus_0();

        System.out.println("nUserRegistCount：" + nUserRegistCount);

        result.put("userRegistCount", Integer.valueOf(nUserRegistCount));
        return result;
    }

    public boolean addRegistApply(UserRegistLog userRegistLog) {
        int count = this.userRegistLogServiceDao.getQuantityByIpAndDay(userRegistLog.getIp(), MyUtil.dateFormatYMD_Sting(new Date()));
        if (count >= 50) {
            logger.error("ip:" + userRegistLog.getId() + " regist limit 50");

            return false;
        }
        this.userRegistLogServiceDao.saveReturnInteger(userRegistLog);

        Map<String, Object> userRegistCountMap = userRegistCountMap();
        for (IoSession as : com.miracle9.game.util.GameParameterAssist.be.values()) {
            AmfMessageSend.storageMethodOperating.notifyAllOperating(new com.miracle9.game.server.message.StorageMethodEntity(as, "userRegistCount", new Object[]{userRegistCountMap}));
        }
        return true;
    }

    public Pager getRegistApplyList(Pager pager, IoSession session) {
        pager = getRegistApplyList(pager);
        return pager;
    }

    public Pager getRegistApplyList(Pager pager) {
        if ((pager.getOrder() == "") && (pager.getOrderBy() == "")) {
            pager.setOrderBy("applyDate");
            pager.setOrder("desc");
        }
        List<Object> datas = new ArrayList();
        pager = this.userRegistLogServiceDao.getByApplyStatus_0(pager);
        for (Object o : pager.getList()) {
            UserRegistLog userRegistLog = (UserRegistLog) o;
            Map<String, Object> data = new HashMap();
            data.put("applyId", Integer.valueOf(userRegistLog.getId()));
            data.put("username", userRegistLog.getUsername());
            data.put("nickname", userRegistLog.getNickname());
            data.put("promoterName", userRegistLog.getPromoterName());
            data.put("phone", userRegistLog.getPhone());
            data.put("applyDate", userRegistLog.getApplyDate());
            datas.add(data);
        }
        pager.setList(datas.toArray());
        return pager;
    }

    public Pager getVerifyLog(String startDate, String endDate, Pager pager, IoSession session) {
        pager = getVerifyLog(startDate, endDate, pager);
        return pager;
    }

    public Pager getVerifyLog(String startDate, String endDate, Pager pager) {
        if ((pager.getOrder() == "") && (pager.getOrderBy() == "")) {
            pager.setOrderBy("operationDate");
            pager.setOrder("desc");
        }
        return this.userRegistLogServiceDao.getByDateInterval(startDate, endDate, pager);
    }

    public Map<String, Object> updateStatus(int nApplyId, int nApplyStatus, IoSession session) {
        Integer adminId = Integer.valueOf(session.getAttribute("id").toString());
        Admin admin = (Admin) this.adminServiceDao.getById(adminId);
        UserRegistLog userRegistLog = (UserRegistLog) this.userRegistLogServiceDao.getById(Integer.valueOf(nApplyId));
        userRegistLog.setApplyStatus(nApplyStatus);
        userRegistLog.setOperatorName(admin.getUsername());
        userRegistLog.setOperationDate(MyUtil.dateFormatYMD_HMS_Sting(new Date()));
        this.userRegistLogServiceDao.update(userRegistLog);
        if (nApplyStatus == 1) {
            SystemConfig systemConfig = systemConfigServiceDao.getSystemConfig();
            User user = new User();
            user.setUsername(userRegistLog.getUsername());
            user.setNickname(userRegistLog.getNickname());
            user.setPhone(userRegistLog.getPhone());
            user.setPassword(userRegistLog.getPassword());
            user.setType(0);
            //如果是机器人代理则临时改为独家代理
            if(systemConfig.getRobotPromoter().equals(userRegistLog.getPromoterName())){
                systemConfig.setProxyMode(0);
            }
            //独家代理
            if (systemConfig.getProxyMode() == 0) {
                user.setPromoterName(userRegistLog.getPromoterName());
                user.setPromoterId(userRegistLog.getPromoterId());
            }
            //全民代理
            if (systemConfig.getProxyMode() == 1) {
                user.setParentId(userRegistLog.getPromoterId());
            }
            this.userService.regist(user, systemConfig);
        }
        Map<String, Object> userRegistCountMap = userRegistCountMap();
        for (IoSession as : com.miracle9.game.util.GameParameterAssist.be.values()) {
            AmfMessageSend.storageMethodOperating.notifyAllOperating(new com.miracle9.game.server.message.StorageMethodEntity(as, "userRegistCount", new Object[]{userRegistCountMap}));
        }
        Map<String, Object> result = new HashMap();
        result.put("success", Boolean.TRUE);
        result.put("message", "");
        return result;
    }

    public void cleanPastRegistLog(String date) {
        this.userRegistLogServiceDao.deletePastResult(date);
    }

    public void cleanRegistApply() {
        this.userRegistLogServiceDao.cleanRegistApply();
    }

}
